package com.haoran.dwz.modules.dwz;

import java.io.Serializable;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.haoran.dwz.domain.entities.ShortURL;
import com.haoran.dwz.rest.R;
import com.haoran.dwz.web.URLController;
import com.haoran.dwz.wrapper.PaginationWrapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(tags = { "DWZ" }, description = "短网址服务", position = 0)
@RequestMapping(path = "kyeopenapi", headers = { "access-token", "kye",
		"Content-Type" }, consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
@ApiImplicitParams({
	@ApiImplicitParam(name = "access-token", value = "可访问接口的令牌", required = true, paramType = "header"),
	@ApiImplicitParam(name = "kye", value = "10001: 官网; 10002: ERP; 10003: 接口部; 10004: 微官网; 10005: 微信", required = true, paramType = "header"),
	@ApiImplicitParam(name = "Content-Type", defaultValue = "application/json;charset=UTF-8", example = "application/json;charset=UTF-8", value = "编码", required = true, paramType = "header") })
public class DwzController extends URLController {

	@ApiOperation(value = "短链接转换成长链接接口", hidden = true)
	@RequestMapping(value = "dwz-parse", method = RequestMethod.POST)
	@ResponseBody
	@ApiImplicitParams({
			@ApiImplicitParam(name = "access-token", value = "可访问接口的令牌", required = true, paramType = "header"),
			@ApiImplicitParam(name = "kye", value = "客户端应用编码", required = true, paramType = "header"),
			@ApiImplicitParam(name = "Content-Type", defaultValue = "application/json;charset=UTF-8", example = "application/json;charset=UTF-8", value = "客户端应用编码", required = true, paramType = "header") })
	public R parse(@RequestBody Dwz dwz) {
		return super.parse(dwz.getUrl());
	}

	/**
	 * <pre>
	&#64;ApiOperation(value="生成短链接记录接口",notes="签名参数access-token生成的方法<br/>" + 
			"第1步: 将所有参数（注意是所有参数），除去值是空的参数，按参数名升序排序。<br/>" + 
			"第2步: 然后把排序后的参数按参数1值1参数2值2…参数n值n（这里的参数和值必须是传输参数的原始值，不能是经过处理的。<br/>" + 
			"第3步: 把分配给接入方的验证密钥key拼接在第2步得到的字符串前面。<br/>" + 
			"第4步: 在上一步得到的字符串 计算md5（32位大写）值，得到的字符串作为access-token的值<br/>")
	&#64;RequestMapping(value = "dwz-generate", method = RequestMethod.POST)
	&#64;ResponseBody
	&#64;ApiImplicitParams(
			{
				&#64;ApiImplicitParam(name = "access-token", value = "可访问接口的令牌", required = true, paramType = "header"),
				&#64;ApiImplicitParam(name = "kye", value = "10001: 官网; 10002: ERP; 10003: 接口部; 10004: 微官网; 10005: 微信", required = true, paramType = "header"),
				&#64;ApiImplicitParam(name = "Content-Type",defaultValue="application/json;charset=UTF-8",example="application/json;charset=UTF-8", value = "客户端应用编码", required = true, paramType = "header")
			})
	 * </pre>
	 */
	@Deprecated
	public R generateX(@RequestBody Dwz dwz) {
		return super.generate(dwz.getUrl());
	}

	@ApiOperation(value = "生成短链接记录接口", notes = "签名参数access-token生成的方法<br/>"
			+ "第1步: 将所有参数（注意是所有参数），除去值是空的参数，按参数名升序排序。<br/>"
			+ "第2步: 然后把排序后的参数按参数1值1参数2值2…参数n值n（这里的参数和值必须是传输参数的原始值，不能是经过处理的。<br/>"
			+ "第3步: 把分配给接入方的验证密钥key拼接在第2步得到的字符串前面。<br/>" + "第4步: 在上一步得到的字符串 计算md5（32位大写）值，得到的字符串作为access-token的值<br/>")
	@RequestMapping(value = "dwz-generate", method = RequestMethod.POST)
	@ResponseBody
	public R generate(@RequestBody Dwz dwz) {
		return super.generate(dwz.getUrl(), dwz.getType());
	}

	/// ↓↓↓↓↓↓↓↓父类不想暴露的接口给重写不标注路由@RequestMapping↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
	
	@Override
	@ApiOperation(value = "", hidden = true)
	public R usedRate(int type) {
		return super.usedRate(type);
	}
	
	@Override
	@ApiOperation(value = "", hidden = true)
	public R generate(String URL, int type) {
		return super.generate(URL, type);
	}
	
	@Override
	@ApiOperation(value = "查询所有的短链接记录",hidden = false)
	@RequestMapping(value = "dwz-list", method = RequestMethod.GET,headers = {"Content-type"})
	@ResponseBody
	public R list() {
		return super.list();
	}

	@Override
	@ApiOperation(value = "", hidden = true)
	public String index(Model model) {
		return super.index(model);
	}

	@Override
	@ApiOperation(value = "", hidden = true)
	public R delete(long[] ids) {
		return super.delete(ids);
	}

	@ApiOperation(value = "", hidden = true)
	@Override
	public PaginationWrapper findByPageable(ShortURL shortURL) {
		return super.findByPageable(shortURL);
	}

	@SuppressWarnings("deprecation")
	@ApiOperation(value = "", hidden = true)
	@Override
	public R generate(String URL) {
		return super.generate(URL);
	}

	@ApiOperation(value = "", hidden = true)
	@Override
	public R parse(String shortURL) {
		return super.parse(shortURL);
	}
	/// ↑↑↑↑↑↑↑↑↑↑父类不想暴露的接口给重写不标注路由@RequestMapping↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

}

@ApiModel("短网址")
class Dwz implements Serializable {
	private static final long serialVersionUID = 1290603183499109274L;
	@ApiModelProperty(value = "网址", required = true)
	private String url;

	@ApiModelProperty(value = "网址类型", required = true, allowableValues = "range[1,5]", notes = "一到五之间的整数", example = "5")
	private int type;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

}
